home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / wc.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  2KB  |  73 lines

  1. /* File: wc.c
  2.  * This file originally appeared in "The C Programming Language" (c) 1978.
  3.  * Modified to become part of MicroEMACS.  Includes line and word information.
  4.  * Searches current buffer only.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "ed.h"
  10.  
  11. wc(f, n)
  12. register int f, n;
  13. {
  14.  
  15.         static double lpw = (0.00);
  16.         static double tp1,tp2;
  17.         register long nc, na, nw;
  18.         register LINE *dlp;
  19.         char lpwbuf[6];
  20.         short dlo, inword;
  21.  
  22.         /*
  23.          * nc = # of characters
  24.          * f = # lines
  25.          * nw = # words
  26.          * na = # alpha characters
  27.          * inword = "Are we in a word?"
  28.         */
  29.  
  30.         inword = NO;
  31.         f = 00;
  32.         na = nw = nc = 0L;
  33.         dlp = curwp->w_dotp;
  34.         dlo = curwp->w_doto;
  35.  
  36.         gotobob(NULL, 1);
  37.  
  38.         while (curwp->w_dotp != curbp->b_linep)
  39.                 {
  40.                 ++nc;
  41.                 n = lgetc(curwp->w_dotp, curwp->w_doto);
  42.                 if (curwp->w_doto == llength(curwp->w_dotp))
  43.                         {
  44.                         ++f;
  45.                         inword = NO;
  46.                         }
  47.                 if (isspace(n))
  48.                         inword = NO;
  49.                 else    if (inword == NO)
  50.                                 {
  51.                                 inword = YES;
  52.                                 ++nw;
  53.                                 }
  54.                 if (isalpha(n))
  55.                         ++na;
  56.                 forwchar(NULL, 1);
  57.                 }
  58.         --nw;
  59.         curwp->w_dotp = dlp;
  60.         curwp->w_doto = dlo;
  61.         curwp->w_flag |= WFHARD;
  62.         if (nw > 0L)            /* avoid division by zero */
  63.                 {
  64.                 tp1 = (double)na;
  65.                 tp2 = (double)nw;
  66.                 lpw = (double)(tp1/tp2);
  67.                 }
  68.         ftoa(lpw, lpwbuf, 2);
  69.         n = mlwrite("Length:=%D Lines:=%d Words:=%D Letters:=%D \
  70. Avg. word:=%s letters", nc, f, nw, na, lpwbuf);
  71.         return(n);
  72. }
  73.